import os
import re
from matplotlib import pyplot as plt

class WindowLengthParser:
    def __init__(self,data_path):
        self.data = [0] * 11
        self.data_path = data_path

    def parse(self):
        for file_name in os.listdir(self.data_path):
            print(file_name + ": ")
            self.parse_file(os.path.join(self.data_path, file_name))

    def parse_file(self,filePath):
        f = open(filePath)
        lines = f.readlines()
        list = [0] * 11
        index = 0
        for line in lines:
            print(line)
            windowPattern = re.search(r'.*Ratio interval between.*nums: (\d+)',line)
            if windowPattern:
                nums = int(windowPattern.group(1))
                list[index] = nums
                index += 1
        self.add_data(list)

    def add_data(self,addList):
        for i in range(11):
            self.data[i] = self.data[i] + addList[i]

    def plot(self):
        x_label = [ round(1 + x * 0.1,1) for x in range(11)]
        plt.bar(range(len(self.data)), self.data, tick_label = x_label)
        plt.xlabel("Time",fontproperties='Times New Roman',fontsize=12)
        plt.ylabel("Nums", fontproperties='Times New Roman', fontsize=12)
        plt.show()